package com.lant.pojo;

import org.springframework.ui.ModelMap;

import com.lant.utils.JsonUtils;
import com.lant.utils.ModelMapUtils;
import com.lant.utils.StringUtils;
import com.lant.utils.XmlUtils;

public abstract class AbstractEntity implements Entity {
	private static final long serialVersionUID = 1L;
	private Long id;
	private Integer version = Integer.valueOf(0);
	private Long lastAccess = Long.valueOf(0L);

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public Long getLastAccess() {
		return lastAccess;
	}

	public void setLastAccess(Long lastAccess) {
		this.lastAccess = lastAccess;
	}

	public String toString() {
		return StringUtils.toObjectString(this);
	}

	public String toXmlString() {
		return XmlUtils.toXmlString(this);
	}

	public String toJsonString() {
		return JsonUtils.toJsonString(this);
	}

	public ModelMap toModelMap() {
		return ModelMapUtils.toModelMap(this);
	}

	public static <T> T fromXmlString(String xml, Class<T> cls) {
		return (T) XmlUtils.parseJavaObject(xml, cls);
	}

	public static <T> T fromJsonString(String json, Class<T> cls) {
		return (T) JsonUtils.parseJavaObject(json, cls);
	}

	public static <T> T fromModelMap(ModelMap model, Class<T> cls) {
		return (T) ModelMapUtils.parseJavaObject(model, cls);
	}
}